# -*- coding: utf-8 -*-
#企业证书字典维护以及对应一级映射维护
import json

#获取全部企业证书字典
def getCompanyCertDictList(request):
    request.cursor_standard.execute(request.formatSql('''
        select * from dict_company_apt  
        where '${key_str}' = '' 
        or lx like '%${key_str}%'  
        or dl like '%${key_str}%'  
        or xl like '%${key_str}%'  
        or zy like '%${key_str}%'
        order by  apt_code 
    '''))
    rows = request.cursor_standard.fetchall()
    return json.dumps(rows, ensure_ascii=False)

#修改一条企业证书字典
def updateCompanyCertDict(request):
    request.cursor_standard.execute(request.formatSql('''
        update dict_company_apt 
        set lx = '${lx}',
        dl = '${dl}',
        xl = '${xl}',
        zy = '${zy}',
        web_show = '${web_show}' 
        where id  = ${id}
    '''))
    return str(request.cursor_standard.rowcount)

#插入一条企业证书字典
def insertCompanyCertDict(request):
    request.cursor_standard.execute(request.formatSql('''
        
    '''))
    return str(request.cursor_standard.rowcount)

#企业证书字典一级映射
def getCertMappingById(request):
    request.cursor_standard.execute(request.formatSql('''
        select * from dict_company_apt_alias where cert_dict_id = ${id}
    '''))
    rows = request.cursor_standard.fetchall()
    return json.dumps(rows, ensure_ascii=False)

#修改企业证书字典一级映射
def updateCertMapping(request):
    request.cursor_standard.execute(request.formatSql('''
        update dict_company_apt_alias 
        set  alias_name = '${alias_name}',
        data_source_num = '${data_source_num}'
        where cert_dict_id = ${cert_dict_id}  
        and alias_id = ${mapping_id}
    '''))
    return str(request.cursor_standard.rowcount)


#新增企业证书字典一级映射
def addCertMapping(request):
    request.cursor_standard.execute(request.formatSql('''
        insert into dict_company_apt_alias(alias_name,cert_dict_id,data_source_num) 
        values('${alias_name}',${cert_dict_id},'${data_source_num}')
    '''))
    return str(request.cursor_standard.rowcount)


#获取企业证书字典一级未能匹配数据
def getCompanyCertUnMatch(request):
    request.cursor_standard.execute(request.formatSql('''
        select distinct original,data_source_code 
        from middle.company_apt_un_match_first 
        order by data_source_code,original
    '''))
    rows = request.cursor_standard.fetchall()
    return json.dumps(rows, ensure_ascii=False)


#删除某数据源企业证书一级匹配未能匹配的记录
def deleteUnMatch(request):
    request.cursor_standard.execute(request.formatSql('''
        delete from  middle.company_apt_un_match_first
        -- where data_source_code = '${data_source_code}'
    '''))
    return str(request.cursor_standard.rowcount)
